namespace PHSRAG.Insight21.Humans
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;

	using PHSRAG.Insight21.Humans;
	using PHSRAG.Insight21.Policy;
	using DataAccess = PHSRAG.Utility.DataAccess;
    using PHSRAG.Insight21.CDS;
    using PHSRAG.Utility;

	/// <summary>
	///	Displays summary information pertaining to the selected protocol
	/// </summary>
	public partial class ProtocolDetailHeader : System.Web.UI.UserControl
	{
		
		private int protocolId;
		private Policy.ICachePolicy	cachePolicy;
		private Protocol protocol;
		
		/// <summary>
		/// Sets the id of the protocol for which details will be displayed
		/// </summary>
		public int ProtocolID
		{
			set
			{
				protocolId = value;
			}
		}

		/// <summary>
		/// Attempts to retrieve a protocol from the cache.  If there, it compares it's ID to the
		/// one maintained, and checks the equality.  If they're different, it loads up a new protocol
		/// based on it, and caches it.  Otherwise, it uses the one in the cache.
		/// 
		/// Then, text is assigned to the labels that make up the control from values on the selected 
		/// protocol, and the event handler (that will update the labels on the event that the Protocol
		/// changes) gets wired up
		/// </summary>
		/// <param name="sender">sender of the event</param>
		/// <param name="e">arguments of the event</param>
		protected void OnPageLoad(object sender, System.EventArgs e)
		{
			try
			{
				if (!IsPostBack)
				{
					/* If the page has set a different protocolId for a protocol to load, 
					 * it will be greater than 0.  If they differ, use the protocolId 
					 * that the page has set to load the protocol*/
					if (protocolId == 0) 
						throw new Exception(
							"The page must set the id of the protocol this control should display details for");

					protocol = new Protocol(cachePolicy);

					object o = cachePolicy.GetCachedObject ( Protocol.ProtocolKey );
					if(( o != null) && (((Protocol)o).Id == protocolId))
						protocol = (Protocol)o;

					else
					{
						protocol.Load(DataAccess.GetConnectionString(), protocolId);
						cachePolicy.Cache(protocol.CacheKey(), protocol);
					}
				}
				else
				{
					protocol = (Protocol)cachePolicy.GetCachedObject(Protocol.ProtocolKey);
				}

				protocol.Change += new PHSRAG.Insight21.Humans.Protocol.ChangeEventHandler(LoadLabels);
			
				LoadLabels();
			}
			catch(Exception ex)
			{
				Utility.ExceptionReport.WriteToEventLog("Protocol Detail Header Page Load", ex, true, Utility.ExceptionReport.ReportType.Error);
			}
		}


		/// <summary>
		/// Sets the text for the labels based on data from the cached protocol
		/// </summary>
		private void LoadLabels()
		{
            protNumber.Text = protocol.ProtocolNumber;
            protNumber.EnableViewState = false;

            title.Text = protocol.TruncatedTitle;
			title.ToolTip = protocol.Title;
			title.EnableViewState = false;
		
			piName.Text = protocol.PIName;
			piName.EnableViewState = false;
		
			status.Text = protocol.OverallStatus;
			status.EnableViewState = false;
		
			sponsorName.Text = protocol.TruncatedSponsorName;
			sponsorName.ToolTip = protocol.SponsorName;
			sponsorName.EnableViewState = false;
	
			approvalDate.Text = protocol.ApprovalDate;
			approvalDate.EnableViewState = false;
		
			expirationDate.Text = protocol.ExpirationDate;
			expirationDate.EnableViewState = false;
		
			administrator.Text = protocol.AdministratorName;
			administrator.EnableViewState = false;
		
			email.Text = protocol.Email;
			email.EnableViewState = false;
		
			phone.Text = protocol.Phone;
			phone.EnableViewState = false;
		
			respIRB.Text = protocol.ResponsibleIRB;
			respIRB.EnableViewState = false;
		
			version.Text = protocol.Version;
			version.EnableViewState = false;
		
			lastModifiedDate.Text = protocol.LastModifiedDate;
			lastModifiedDate.EnableViewState = false;

            if (protocol.ModuleType == Constants.ModuleType.Animals)
            {
                if (!String.IsNullOrEmpty(protocol.SpeciesCode))
                {
                    ICachePolicy applicationCachePolicy = (ICachePolicy)Application[Policy.Names.ApplicationCachePolicyKey];
                    CDS.RefCodes refCodes = (RefCodes)applicationCachePolicy.GetCachedObject(RefCodes.CacheKey);
                    RefCode speciesRefCode = refCodes.GetRefCode(Humans.Constants.AnimalSpecies.Domain, protocol.SpeciesCode);
                    species.Text = speciesRefCode.Meaning;
                }

                versionContainer.Visible = 
                    respIRBContainer.Visible = false;
                periodDateRangeContainer.Visible = 
                    hazardousAgentsContainer.Visible = true;
                numberOfAnimals.Text = protocol.NumberOfAnimals.ToString( );
                if ( protocol.ProtocolPeriodStartDate != DateTime.MinValue )
                    periodStartDate.Text = protocol.ProtocolPeriodStartDate.ToString ();
                if (protocol.ProtocolPeriodEndDate != DateTime.MinValue)
                    periodEndDate.Text = protocol.ProtocolPeriodEndDate.ToString();
                radioIsotope.Text = protocol.RadioIsotope ? "Yes" : "No";
                biologicalAgent.Text = protocol.BiologicalAgent ? "Yes" : "No";
                toxicChemicals.Text = protocol.ToxicChem ? "Yes" : "No";
                tissueTesting.Text = protocol.TissueTest ? "Yes" : "No";
            }
		}

		#region Web Form Designer generated code

		/// <summary>
		/// Initializes the page
		/// </summary>
		/// <param name="e">arguments from the event</param>
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);

			cachePolicy = (Policy.ICachePolicy)Session[Policy.Names.CachePolicyKey];

		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.OnPageLoad);

		}
		#endregion
	}
}
